# 修复常见的编译错误
# 用法: .\fix-common-errors.ps1

# 配置
$REMOTE_SERVER = "root@192.168.50.160"
$REMOTE_PROJECT_PATH = "/home/chenhg/gitee/xsan"

Write-Host "正在检查并修复常见编译错误..." -ForegroundColor Green

# 1. 修复 spdk_manager.c 中的 va_start/va_end 未声明错误
Write-Host "1. 修复 spdk_manager.c 中的 va_start/va_end 未声明错误" -ForegroundColor Yellow
$spdk_manager_file = "src/core/spdk_manager.c"

if (Test-Path $spdk_manager_file) {
    $content = Get-Content $spdk_manager_file -Raw
    
    if ($content -notmatch "#include\s+<stdarg\.h>") {
        # 添加缺失的头文件
        $content = $content -replace "#include\s+<stdio\.h>", "#include <stdio.h>`n#include <stdarg.h>"
        Set-Content $spdk_manager_file $content -NoNewline
        Write-Host "  - 已添加 <stdarg.h> 头文件" -ForegroundColor Green
    } else {
        Write-Host "  - <stdarg.h> 头文件已存在，无需修复" -ForegroundColor Cyan
    }
} else {
    Write-Host "  - 文件 $spdk_manager_file 不存在" -ForegroundColor Red
}

# 2. 修复 xsan_bdev.h 中的 xsan_error_t 未定义错误
Write-Host "2. 修复 xsan_bdev.h 中的 xsan_error_t 未定义错误" -ForegroundColor Yellow
$bdev_header_file = "src/include/xsan_bdev.h"

if (Test-Path $bdev_header_file) {
    $content = Get-Content $bdev_header_file -Raw
    
    if ($content -notmatch "#include\s+[\"'].*xsan_error\.h[\"']") {
        # 添加缺失的头文件
        if ($content -match "#ifndef\s+XSAN_BDEV_H\s*\n#define\s+XSAN_BDEV_H") {
            $content = $content -replace "(#ifndef\s+XSAN_BDEV_H\s*\n#define\s+XSAN_BDEV_H)", "`$1`n`n#include ""../../include/xsan_error.h"""
            Set-Content $bdev_header_file $content -NoNewline
            Write-Host "  - 已添加 xsan_error.h 头文件" -ForegroundColor Green
        } else {
            Write-Host "  - 文件结构不符合预期，请手动添加 xsan_error.h" -ForegroundColor Yellow
        }
    } else {
        Write-Host "  - xsan_error.h 头文件已存在，无需修复" -ForegroundColor Cyan
    }
} else {
    Write-Host "  - 文件 $bdev_header_file 不存在" -ForegroundColor Red
}

# 3. 检查其他常见问题
Write-Host "3. 同步修复后的文件到远程服务器" -ForegroundColor Yellow
# 将特定文件同步到远程服务器
scp -r "$bdev_header_file" "${REMOTE_SERVER}:$REMOTE_PROJECT_PATH/$bdev_header_file"
scp -r "$spdk_manager_file" "${REMOTE_SERVER}:$REMOTE_PROJECT_PATH/$spdk_manager_file"

Write-Host "已完成常见编译错误的检查和修复。请运行 .\sync-and-build.ps1 重新构建项目。" -ForegroundColor Green 